//
//  LJTag.m
//  LJClientKit
//
//  Created by che on 13/7/09.
//  Copyright 2009 AntonChebotaev. All rights reserved.
//

#import "LJTag.h"

// Exceptions
NSString * const LJTagBadInitException = @"LJTagBadInitException";

@implementation LJTag

@synthesize name=_name;
@synthesize uses=_uses;

- (id)init {
	[self dealloc];
    @throw [NSException exceptionWithName:LJTagBadInitException
								   reason:@"Initialize LJTag with simple init without tag name"
								 userInfo:nil];
    return nil;	
}

- (id)initWithName:(NSString *)name uses:(int)usesCount {
	self = [super init];
	if (self) {
        _name = [name retain];
        _uses = usesCount;
    }
    return self;
}

- (id)initWithName:(NSString *)name {
	return [self initWithName:name uses:0];
}

+ (id)tagWithName:(NSString *)name {
	return [[[LJTag alloc] initWithName:name] autorelease];
}
+ (id)tagWithName:(NSString *)name uses:(int)usesCount {
	return [[[LJTag alloc] initWithName:name uses:usesCount] autorelease];
}

- (void)dealloc {
	[_name release];
	[super dealloc];
}

- (id)initWithCoder:(NSCoder *)decoder {
	self = [super init];
    if (self) {
        _name = [[decoder decodeObjectForKey:@"LJTagCodingName"] retain];
		_uses = [decoder decodeIntForKey:@"LJTagCodingUses"];

        NSAssert(_name != nil, @"LJTag decoded without a name.");
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:_name forKey:@"LJTagCodingName"];
	[encoder encodeInt:_uses forKey:@"LJTagCodingUses"];
}

- (NSString *)description {
	return _name;
}

- (NSUInteger)hash {
	return [[self description] hash];
}

- (BOOL)isEqual:(id)object {
	return [object isMemberOfClass:[LJTag class]] && [[self description] isEqual:[object description]];
}
@end
